# 导入cv模块
import cv2 as cv

faceCascade = cv.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
eyeCascade = cv.CascadeClassifier('Cascades/haarcascade_eye.xml')
# 读取图像，支持 bmp、jpg、png、tiff 等常用格式
img = cv.imread(r"1.jpeg")
# 创建窗口并显示图像
cv.namedWindow("Image")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.3,
    minNeighbors=5,
    minSize=(30, 30)
)

print("发现{0}个人脸!".format(len(faces)))

for (x, y, w, h) in faces:
    for (x, y, w, h) in faces:
        cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = img[y:y + h, x:x + w]
    eyes = eyeCascade.detectMultiScale(
        roi_gray,
        scaleFactor=2.0,
        minNeighbors=10,
        minSize=(5, 5),
    )
    for (ex, ey, ew, eh) in eyes:
        cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
    cv.imshow("Image", img)
cv.waitKey(0)
# 释放窗口
cv.destroyAllWindows()
